define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shopro/third/doctors/index' + location.search,
                    add_url: 'shopro/third/doctors/add',
                    edit_url: 'shopro/third/doctors/edit',
                    del_url: 'shopro/third/doctors/del',
                    multi_url: 'shopro/third/doctors/multi',
                    import_url: 'shopro/third/doctors/import',
                    table: 'shopro_third_doctors',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'doctor_name', title: __('Doctor_name'), operate: 'LIKE'},
                        {field: 'hospital', title: __('Hospital'), operate: 'LIKE'},
                        {field: 'users_total', title: __('Users_total')},
                        {field: 'order_total', title: __('Order_total')},
                        {field: 'total_consume', title: __('Total_consume'), operate:'BETWEEN'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {   
                                    name: 'wz_button',
                                    text: '小程序码',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-qrcode',
                                    url: function (row) {
                                        return Fast.api.fixurl('shopro/third/doctors/wxcode?user_id=' + row.user_id)
                                    },  
                                    extend: 'data-area=\'["300px","500px"]\'',
                                },  
                                {   
                                    name: 'wz_button',
                                    text: '成单详情',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa',
                                    url: function (row) {
                                        return Fast.api.fixurl('shopro/third/doctors/orders?user_id=' + row.user_id)
                                    },  
                                    extend: 'data-area=\'["80%","80%"]\'',
                                },  
                            ],  
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
            });

            $(document).on("click", ".btn-export", function () {
                window.open(Fast.api.fixurl('shopro/third/doctors/exportdoctor' + location.search))
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        wxcode: function () {
            Controller.api.bindevent();
        },
        orders: function () {
            $(document).on("click", ".btn-export", function () {
                window.open(Fast.api.fixurl('shopro/third/doctors/exportorder' + location.search))
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
